package com.feidee.data.report.util;


import com.feidee.data.report.constant.Constant;
import com.feidee.fdcommon.configuration.CustomConfiguration;
import org.apache.log4j.Logger;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class DbOperationUtil {

    private static Logger logger = Logger.getLogger(DbOperationUtil.class);

    public static DataSource cusMysqlDataSource;
    public static DataSource sensitiveDataSource;

    /**
     * 获取结果表Mysql数据库连接
     *
     * @param isCRFlag
     * @return
     */
    public static Connection getMysqlConntion(String isCRFlag) throws Exception {
        //Map<String, String> dbInfo = null;
        if ("0".equals(isCRFlag)) {
            // 按安全部署模式 获取数据库信息
            logger.info("Mysql敏感库连接信息 baseinfo:" + CustomConfiguration.getString(Constant.MYSQL_BASE_INFO));
            /*dbInfo = JsonUtil.jsonToStrMap(
                    SimpleAES.decrypt(CustomConfiguration.getString(Constant.MYSQL_BASE_INFO).trim(), Constant.MYSQL_DECRYPT_KEY));*/
            return sensitiveDataSource.getConnection();
        } else {
            String driverStr = CustomConfiguration.getString(Constant.CUSTOM_REPORT_MYSQL_DRIVER, "com.mysql.jdbc.Driver");
            String urlStr = CustomConfiguration.getString(Constant.CUSTOM_REPORT_MYSQL_URL,
                    "jdbc:mysql://10.195.1.151:3306/data_report?useUnicode=true&amp;characterEncoding=utf8");
            String userStr = CustomConfiguration.getString(Constant.CUSTOM_REPORT_MYSQL_USER, "PT_super_user");
            String passwdStr = CustomConfiguration.getString(Constant.CUSTOM_REPORT_MYSQL_PASSWD, "Hb86be03cc_H3,,2c8ff86#326c3");
            logger.info("Mysql非敏感库连接信息,Driver: " + driverStr + " ,Url: " + urlStr + " ,User: " + userStr
                    + " ,Password: " + passwdStr);
            return cusMysqlDataSource.getConnection();
        }
        //return JdbcManager.getConnection(dbInfo);
    }

    public static void main(String[] args) throws Exception {
        System.out.println(SimpleAES.decrypt("cd2a88683a7a90467bfe110becc91693c65b625f3354b6186ab55f5b38f53f74055e9aa39f63c8a5dd7fba4060448ba2c15bc541f2f58a1ce97baf7071bd099963c4b5498c89aac6941b68f0b50025596e21b17a3b82f511b67a0d5667872fa5c0fd297e052d1614b3c70bfa5ac535fc6164f420ef890719dd77d967e74e671fbf5967324070a3618750fac093b69d7516eedab2a5f2ecf8280737b64bbc24da43413ce11b1e7069865d5c2e04eea53a316503496f08d5f20493add999a6c9b837e13755bd3531c06ca55f37679989228b2cb94c61a30cbc899ed51ddfae2fc94ff0ffedc966634f1d2bd332047aa25e791e14404c6b428ed4b7baba279ab80cca635d3a1020e8917ec7d7e66890a2d4cdee9344bdc73c68b4025f8b52bf205d", "c7c2H_7_dbHb8fMMMMec8d5mykey52d85bc6"));
    }

    /**
     * 清理数据库连接环境
     *
     * @param rs
     */
    public static void cleanConnEnv(ResultSet rs) {
        try {
            if (rs != null)
                rs.close();
        } catch (SQLException e) {
            logger.info(e);
        }
    }

    /**
     * 清理数据库连接环境
     *
     * @param stmt
     */
    public static void cleanConnEnv(Statement stmt) {
        try {
            if (stmt != null)
                stmt.close();
        } catch (SQLException e) {
            logger.info(e);
        }
    }

    /**
     * 清理数据库连接环境
     *
     * @param conn
     * @param stmt
     * @param rs
     */
    public static void cleanConnEnv(Connection conn, Statement stmt, ResultSet rs) {
        try {
            if (rs != null)
                rs.close();
            if (stmt != null)
                stmt.close();
            if (conn != null)
                conn.close();
        } catch (SQLException e) {
            logger.info(e);
        }
    }

    /**
     * 清理数据库连接环境
     *
     * @param conn
     * @param stmt
     */
    public static void cleanConnEnv(Connection conn, Statement stmt) {
        try {
            if (stmt != null)
                stmt.close();
            if (conn != null)
                conn.close();
        } catch (SQLException e) {
            logger.info(e);
        }
    }

    /**
     * 清理数据库连接环境
     *
     * @param stmt
     * @param rs
     */
    public static void cleanConnEnv(Statement stmt, ResultSet rs) {
        try {
            if (rs != null)
                rs.close();
            if (stmt != null)
                stmt.close();
        } catch (SQLException e) {
            logger.info(e);
        }
    }

    public void setCusMysqlDataSource(DataSource cusMysqlDataSource) {
        DbOperationUtil.cusMysqlDataSource = cusMysqlDataSource;
    }

    public void setSensitiveDataSource(DataSource sensitiveDataSource) {
        DbOperationUtil.sensitiveDataSource = sensitiveDataSource;
    }
}